/**
 * 
 */
package de.alex.schulzeit.db;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import de.alex.schulzeit.db.fixture.FixtureTable;
import de.alex.schulzeit.db.subject.SubjectTable;
import de.alex.schulzeit.db.subjecttable.TableSubjectTable;
import de.alex.schulzeit.db.teacher.TeacherTable;
import de.alex.schulzeit.db.timetable.TimetableTable;

/**
 * Provides necessary Database + Functions to create and upgrade
 * 
 * @author Alexander Ruetz
 *
 */
public class DatabaseHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "schulzeit.db";
	private static final int DATABASE_VERSION = 1;
	private static final String TAG = DatabaseHelper.class.getSimpleName();
	
	/**
	 * @param context
	 */
	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		try {
			db.execSQL(FixtureTable.SQL_CREATE);
			db.execSQL(SubjectTable.SQL_CREATE);
			db.execSQL(TeacherTable.SQL_CREATE);
			db.execSQL(TimetableTable.SQL_CREATE);
			db.execSQL(TableSubjectTable.SQL_CREATE);
			createDummyData(db);
			
		} catch (SQLException e) {
			// TODO: handle exception
			
		}

	}

	/* (non-Javadoc)
	 * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(FixtureTable.SQL_DROP);
		db.execSQL(SubjectTable.SQL_DROP);
		db.execSQL(TeacherTable.SQL_DROP);
		db.execSQL(TimetableTable.SQL_DROP);
		db.execSQL(TableSubjectTable.SQL_DROP);
		onCreate(db);

	}
	
	public void createDummyData(SQLiteDatabase db) {
		db.execSQL(TeacherTable.SQL_DUMMY1);
		db.execSQL(TeacherTable.SQL_DUMMY2);
		db.execSQL(TeacherTable.SQL_DUMMY3);
		db.execSQL(TeacherTable.SQL_DUMMY4);
		db.execSQL(SubjectTable.SQL_DUMMY1);
		db.execSQL(SubjectTable.SQL_DUMMY2);
		db.execSQL(SubjectTable.SQL_DUMMY3);
		db.execSQL(SubjectTable.SQL_DUMMY4);
		db.execSQL(TimetableTable.SQL_DUMMY1);
		db.execSQL(TableSubjectTable.SQL_DUMMY1);
		db.execSQL(TableSubjectTable.SQL_DUMMY2);
		db.execSQL(TableSubjectTable.SQL_DUMMY3);
		db.execSQL(TableSubjectTable.SQL_DUMMY4);
		db.execSQL(FixtureTable.SQL_DUMMY1);
		db.execSQL(FixtureTable.SQL_DUMMY2);
	}
	
}
